package net.taviscaron.drivenotes.android.activity;

import net.taviscaron.drivenotes.android.R;
import net.taviscaron.drivenotes.android.service.ServiceService;
import net.taviscaron.drivenotes.shared.model.entity.Car;
import net.taviscaron.drivenotes.shared.model.entity.Service;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ServiceActivity extends ListActivity {
    private ServiceListAdapter adapter;
    private ServiceService serviceService;
    private Car car;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new ServiceListAdapter();
        setListAdapter(adapter);
        
        serviceService = new ServiceService(this);
        car = (Car)getIntent().getSerializableExtra("car");
    }
    
    @Override
    protected void onStart() {
	super.onStart();
	if(adapter.getService() == null) {
	    if(serviceService.isOnline()) {
		new ServiceLoader().execute();
	    } else {
		Toast.makeText(this, "You need an internet connection", Toast.LENGTH_SHORT).show();
	    }
	}
    }
    
    private class ServiceListAdapter extends BaseAdapter {
	private Service[] service;
	
	public Service[] getService() {
	    return service;
	}
	
	public void setService(Service[] service) {
	    this.service = service;
	    notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
	    return (service != null) ? service.length : 0;
	}

	@Override
	public Service getItem(int position) {
	    return service[position];
	}

	@Override
	public long getItemId(int position) {
	    return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup viewGroup) {
	    if(view == null) {
		LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view = layoutInflater.inflate(R.layout.service_item, null);
	    }
	    
	    Service service = getItem(position);
	    
	    ((TextView)view.findViewById(R.id.serviceProblems)).setText(service.getProblems());
	    ((TextView)view.findViewById(R.id.serviceSpares)).setText(service.getSpares());
	    ((TextView)view.findViewById(R.id.serviceCost)).setText(numberToString(service.getServiceCost()));
	    ((TextView)view.findViewById(R.id.serviceSparesCost)).setText(numberToString(service.getSparesCost()));
	    ((TextView)view.findViewById(R.id.serviceMileage)).setText(numberToString(service.getOdometrValue()));
	    ((TextView)view.findViewById(R.id.serviceDate)).setText(service.getDate().toString());
	    ((TextView)view.findViewById(R.id.serviceComment)).setText(service.getComment());

	    return view;
	}
	
	private String numberToString(Number number) {
	    return (number == null) ? "0" : number.toString();
	}
    }
    
    private class ServiceLoader extends AsyncTask<Void, Void, Service[]> {
	private ProgressDialog dialog;
	@Override
	protected Service[] doInBackground(Void... args) {
	    return serviceService.loadService(car);
	}
	
	@Override
	protected void onPreExecute() {
	    dialog = ProgressDialog.show(ServiceActivity.this, "", "Loading...");
	}
	
	@Override
	protected void onPostExecute(Service[] result) {
	    dialog.dismiss();
	    if(result == null || result.length == 0) {
		Toast.makeText(ServiceActivity.this, "There is no service for this car", Toast.LENGTH_LONG).show();
	    } else {
		adapter.setService(result);
	    }
	}
    }
}
